/**
 * 项目名：	Rumba Plan
 * 
 * 版权所有(C)，上海海鼎信息工程股份有限公司，2006，所有权利保留。
 * 
 * 文件名：	BSQLSubquery.java
 * 模块说明：	
 * 修改历史：
 * 2006-7-11 - lxm - 创建。
 */
package com.hd123.rumba.sql.biz;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.hd123.rumba.exception.BusinessException;
import com.hd123.rumba.sql.SCE;
import com.hd123.rumba.sql.SQLSubquery;

/**
 * @author lxm
 * 
 */
public class BSQLSubquery implements Serializable {
  private static final long serialVersionUID = 300100L;

  private Boolean isDistinct = Boolean.FALSE;
  private List<List> selectClause = new ArrayList();
  private List<List> fromClause = new ArrayList();
  private SCE whereCondition = null;
  private List<List> groupClause = new ArrayList();
  private SCE havingCondition = null;
  private List<List> orderClause = new ArrayList();

  public SQLSubquery createSQLSubquery() throws BusinessException {
    SQLSubquery sq = new SQLSubquery();
    assignSelectClause(sq);
    assignFromClause(sq);
    sq.setWhere(whereCondition);
    assignGroupClause(sq);
    sq.setHaving(havingCondition);
    assignOrderClause(sq);
    return sq;
  }

  public void addSelect(String name) {
    List sqp = new ArrayList();
    sqp.add(name);
    selectClause.add(sqp);
  }

  public void addFrom(String name) {
    List sqp = new ArrayList();
    sqp.add(name);
    fromClause.add(sqp);
  }

  public void addFrom(String name, String alias) {
    List sqp = new ArrayList();
    sqp.add(name);
    sqp.add(alias);
    fromClause.add(sqp);
  }

  public void addGroup(String name) {
    List sqp = new ArrayList();
    sqp.add(name);
    groupClause.add(sqp);
  }

  public void addOrder(String name) {
    List sqp = new ArrayList();
    sqp.add(name);
    orderClause.add(sqp);
  }

  public void addOrder(String name, String order) {
    List sqp = new ArrayList();
    sqp.add(name);
    sqp.add(order);
    orderClause.add(sqp);
  }

  public List<List> getFromClause() {
    return fromClause;
  }

  public void setFromClause(List<List> fromClause) {
    this.fromClause = fromClause;
  }

  public List<List> getGroupClause() {
    return groupClause;
  }

  public void setGroupClause(List<List> groupClause) {
    this.groupClause = groupClause;
  }

  public SCE getHavingCondition() {
    return havingCondition;
  }

  public void setHavingCondition(SCE havingCondition) {
    this.havingCondition = havingCondition;
  }

  public Boolean getIsDistinct() {
    return isDistinct;
  }

  public void setIsDistinct(Boolean isDistinct) {
    this.isDistinct = isDistinct;
  }

  public List<List> getOrderClause() {
    return orderClause;
  }

  public void setOrderClause(List<List> orderClause) {
    this.orderClause = orderClause;
  }

  public List<List> getSelectClause() {
    return selectClause;
  }

  public void setSelectClause(List<List> selectClause) {
    this.selectClause = selectClause;
  }

  public SCE getWhereCondition() {
    return whereCondition;
  }

  public void setWhereCondition(SCE whereCondition) {
    this.whereCondition = whereCondition;
  }

  private void assignSelectClause(SQLSubquery sq) throws BusinessException {
    for (int i = 0; i < selectClause.size(); i++) {
      if (selectClause.get(i).size() == 0)
        continue;
      String name = (String) selectClause.get(i).get(0);
      if (i == 0 && Boolean.TRUE.equals(isDistinct))
        name = "distinct " + name;
      if (selectClause.get(i).size() == 1) {
        sq.addSelect(name);
        continue;
      }
      String alias = (String) selectClause.get(i).get(1);
      sq.addSelect(name, alias);
    }
  }

  private void assignFromClause(SQLSubquery sq) throws BusinessException {
    for (int i = 0; i < fromClause.size(); i++) {
      if (fromClause.get(i).size() == 0)
        continue;
      String name = (String) fromClause.get(i).get(0);
      if (fromClause.get(i).size() == 1) {
        sq.addFrom(name);
        continue;
      }
      String alias = (String) fromClause.get(i).get(1);
      sq.addFrom(name, alias);
    }
  }

  private void assignGroupClause(SQLSubquery sq) throws BusinessException {
    for (int i = 0; i < groupClause.size(); i++) {
      if (groupClause.get(i).size() == 0)
        continue;
      String name = (String) groupClause.get(i).get(0);
      sq.addGroup(name);
    }
  }

  private void assignOrderClause(SQLSubquery sq) throws BusinessException {
    for (int i = 0; i < orderClause.size(); i++) {
      if (orderClause.get(i).size() == 0)
        continue;
      String name = (String) orderClause.get(i).get(0);
      if (orderClause.get(i).size() == 1) {
        sq.addOrder(name);
        continue;
      }
      String order = (String) orderClause.get(i).get(1);
      sq.addOrder(name, order);
    }
  }
}
